<!--车辆维修页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding: 10px 20px">
    <span style="line-height: 60px;font-size: 20px;">车辆维修</span>
    <el-button type="primary" style="float:right;margin-top: 13px;" @click="showDialog">录入维修车辆</el-button>
  </div>
  <!-- 录入维修车辆弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px" :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="送修人">
            <el-input readonly v-model="repairForm.username"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修车辆">
            <el-select placeholder="请选择车牌号" v-model="repairForm.license">
              <el-option v-for="item in licenseArr" :label="item" :value="item"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修项目">
            <el-select placeholder="请选择" v-model="repairForm.item">
              <el-option v-for="item in itemArr" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修地点">
            <el-select placeholder="请选择" v-model="repairForm.addr">
              <el-option v-for="item in addrArr" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修材料">
            <el-select placeholder="请选择" v-model="repairForm.material">
              <el-option v-for="item in materialArr" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修费用">
            <el-input placeholder="请输入" v-model="repairForm.fee"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="repairForm.repairTime"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修状态">
            <el-radio-group v-model="repairForm.status">
              <el-radio link label="维修中" value="1"></el-radio>
              <el-radio link label="已完成" value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveRepair">录入</el-button>
    </template>
  </el-dialog>
  <!-- 维修单搜索卡片 -->
  <el-card style="margin: 20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="维修车辆">
        <el-input placeholder="请输入车牌号" style="width:220px;" v-model="searchForm.license" @keydown.enter="loadRepair"></el-input>
      </el-form-item>
      <el-form-item label="送修人员">
        <el-input placeholder="请输入送修员姓名" style="width:220px;" v-model="searchForm.username" @keydown.enter="loadRepair"></el-input>
      </el-form-item>
      <el-form-item label="维修状态" style="width:290px;">
        <el-select placeholder="请选择" v-model="searchForm.status" @change="loadRepair">
          <el-option label="维修中" value="1"/>
          <el-option label="已完成" value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadRepair">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 维修单列表 -->
  <el-card style="margin: 20px;">
    <el-table :data="repairArr">
      <el-table-column type="index" label="编号" width="80" align="center" />
      <el-table-column prop="license" label="维修车辆" align="center" />
      <el-table-column prop="item" label="维修项目" align="center" :formatter="itemFormatter"/>
      <el-table-column prop="username" label="送检人员" align="center" />
      <el-table-column prop="addr" label="维修地点" align="center" :formatter="addrFormatter"/>
      <el-table-column prop="repairTime" label="维修时间" align="center" />
      <el-table-column prop="fee" label="维修费用" align="center" />
      <el-table-column prop="material" label="维修材料" align="center" :formatter="materialFormatter" />
      <el-table-column prop="status" label="维修状态" align="center" :formatter="statusFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editRepair(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteRepair(scope.row.id)" :disabled="scope.row.status==0">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";
//定义变量控制dialog的标题
const dialogTitle = ref('录入维修车辆');
//定义变量控制录入维修车辆弹窗是否显示
const dialogVisible = ref(false);
//定义对象用来保存维修单的搜索条件
const searchForm = ref({
  license: '',
  username: '',
  status: ''
});
//定义数组对象用来保存维修单列表
const repairArr = ref([]);
//准备数组用来保存维修项目的字典项
const itemArr = ref([]);
//准备数组用来保存维修地点的字典项
const addrArr = ref([]);
//准备数组用来保存维修材料的字典项
const materialArr = ref([]);
//准备数组用来保存维修状态的字典项
const statusArr = ref([]);

onMounted(()=>{
  loadDictOptions(itemArr,'repair_item');
  loadDictOptions(addrArr,'repair_addr');
  loadDictOptions(materialArr,'repair_material');
  loadDictOptions(statusArr,'repair_status');
  loadRepair();
  loadFreeVehicle();
})
//对维修项目表格进行显示文字转换
const itemFormatter = (row,column,cellValue,index) => {
  if(!itemArr.value){
    return cellValue;
  }
  for(let item of itemArr.value){
    if(item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}
//对维修地点表格进行显示文字转换
const addrFormatter = (row,column,cellValue,index) => {
  if(!addrArr.value){
    return cellValue;
  }
  for(let item of addrArr.value){
    if(item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}
//对维修材料表格进行显示文字转换
const materialFormatter = (row,column,cellValue,index) => {
  if(!materialArr.value){
    return cellValue;
  }
  for(let item of materialArr.value){
    if(item.value == cellValue){
      cellValue = item.label;
      console.log(cellValue)
    }
  }
  return cellValue;
}
//对维修状态表格进行显示文字转换
const statusFormatter = (row,column,cellValue,index) => {
  console.log(repairArr.value)
  if(!statusArr.value){
    return cellValue;
  }
  for(let item of statusArr.value){
    if(item.value == cellValue){
      cellValue = item.label;
      console.log(cellValue)
    }
  }
  return cellValue;
}
//定义加载维修单列表方法
const loadRepair = () => {
  console.log(1);
  let data = qs.stringify(searchForm.value);
  axios.get(BASE_URL+'/v1/repair/select?'+data).then((response)=>{
    if(response.data.code==2000){
      repairArr.value = response.data.data;
      console.log(repairArr.value);
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}

//定义重置搜索的方法
const resetSearch = () => {
  searchForm.value = {};
  loadRepair();
}
//定义数组保存所有的车牌号
const licenseArr = ref([]);
//获取所有的空闲车辆
const loadFreeVehicle = () => {
  axios.get(BASE_URL+'/v1/vehicle/select?status=1').then((response)=>{
    if(response.data.code==2000){
      for(let item of response.data.data){
        licenseArr.value.push(item.license);
      }
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//获取当前登录的用户数据
const user = ref(getUser());
//定义对象用来保存录入维修车辆弹窗的所有数据
const repairForm = ref({
  username: '',
  license: '',
  item: '',
  addr: '',
  material: '',
  fee: '',
  repairTime: '',
  status: ''
})
//定义展示录入维修车辆弹窗
const showDialog = () => {
  dialogVisible.value = true;
  dialogTitle.value = '录入维修车辆';
  repairForm.value = {};
  repairForm.value.username = user.value.username;
}
//定义保存维修单的方法
const saveRepair = () => {
  console.log(repairForm.value);
  //对数据进行校验
  if(!repairForm.value.license){
    ElMessage.error('请选择维修车辆车牌号!');
    return;
  }
  if(!repairForm.value.item){
    ElMessage.error('请选择维修项目!');
    return;
  }
  if(!repairForm.value.addr){
    ElMessage.error('请选择维修地点!');
    return;
  }
  if(!repairForm.value.material){
    ElMessage.error('请选择维修材料!');
    return;
  }
  if(!repairForm.value.fee){
    ElMessage.error('请填写维修费用!');
    return;
  }
  if(!repairForm.value.repairTime){
    ElMessage.error('请选择维修时间!');
    return;
  }
  if(!repairForm.value.status){
    ElMessage.error('请选择维修状态!');
    return;
  }
  let data = qs.stringify(repairForm.value);
  axios.post(BASE_URL+'/v1/repair/save',data).then((response)=>{
    if(response.data.code==2000){
      ElMessage.success('保存成功!');
      dialogVisible.value = false;
      repairForm.value={};
      loadRepair();
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//定义关闭录入维修车辆弹窗的方法
const handleClose = () => {
  if(confirm('您确认要关闭本弹窗吗？')){
    dialogVisible.value = false;
    repairForm.value={};
  }
}
//定义编辑维修单的方法
const editRepair = (id) => {
  dialogTitle.value = '编辑维修车辆';
  dialogVisible.value = true;
  axios.get(BASE_URL+'/v1/repair/select?id='+id).then((response)=>{
    if(response.data.code==2000){
      repairForm.value = response.data.data[0];
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//定义删除维修单的方法
const deleteRepair = (id) => {
  if(confirm('您确认要删除本条记录吗？')){
    axios.post(BASE_URL+'/v1/repair/delete/'+id).then((response)=>{
      if(response.data.code==2000){
        ElMessage.success('删除成功!');
        loadRepair();
      }else{
        ElMessage.error(response.data.msg);
      }
    })
  }
}
</script>
